package com.feng.controller;

import com.feng.anno.Log;
import com.feng.pojo.Dept;
import com.feng.pojo.Result;
import com.feng.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    //查询部门数据
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        //调用servlet查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    //根据id删除
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    //新增部门
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    //根据id查询
    @Log
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询:{}",id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    //修改部门
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门信息:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
